{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# FSM - User can input speaker transition constraints\n",
    "\n",
    "AG2 offers conversable agents powered by LLMs, tools, or humans, which can be used to perform tasks collectively via automated chat. This framework allows tool use and human participation through multi-agent conversation.\n",
    "Please find documentation about this feature [here](https://docs.ag2.ai/latest/docs/user-guide/basic-concepts/conversable-agent/).\n",
    "\n",
    "This notebook is about using graphs to define the transition paths amongst speakers.\n",
    "\n",
    "Benefits\n",
    "- This contribution fills the gap between the current modes of GroupChat Class (auto, manual, round_robin) and an expressive directed graph. See Motivation for more detailed discussion.\n",
    "\n",
    "\n",
    "````{=mdx}\n",
    ":::info Requirements\n",
    "Install `ag2`:\n",
    "```bash\n",
    "pip install ag2[openai]\n",
    "```\n",
    "\n",
    "For more information, please refer to the [installation guide](https://docs.ag2.ai/latest/docs/user-guide/basic-concepts/installing-ag2).\n",
    ":::\n",
    "````"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%capture --no-stderr\n",
    "%pip install ag2[openai,graph]>=0.2.11"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import random  # noqa E402\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "import networkx as nx\n",
    "\n",
    "import autogen\n",
    "from autogen.agentchat.conversable_agent import ConversableAgent\n",
    "from autogen.agentchat.assistant_agent import AssistantAgent\n",
    "from autogen.agentchat.groupchat import GroupChat\n",
    "from autogen.graph_utils import visualize_speaker_transitions_dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(autogen.__version__)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Motivation\n",
    "\n",
    "\n",
    "The current GroupChat class allows transitioning to any agent (with or without the decision of the LLM), some use cases might demand for more control over transition. A graph is a possible way to control the transition paths, where each node represents an agent and each directed edge represents possible transition paths. Let's illustrate the current transition paths for a GroupChat with five agents."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "filter_dict = {\"tags\": [\"gpt-4\", \"gpt-4-32k\"]}\n",
    "llm_config = autogen.LLMConfig.from_json(path=\"OAI_CONFIG_LIST\", timeout=600, cache_seed=44, temperature=0).where(\n",
    "    **filter_dict  # comment out to get all\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "agents = [ConversableAgent(name=f\"Agent{i}\", llm_config=False) for i in range(5)]\n",
    "allowed_speaker_transitions_dict = {agent: list(agents) for agent in agents}\n",
    "\n",
    "visualize_speaker_transitions_dict(allowed_speaker_transitions_dict, agents)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Possibly interesting transition paths\n",
    "1. Hub and Spoke\n",
    "2. Sequential Team Operations\n",
    "3. Think aloud and debate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "agents = [ConversableAgent(name=f\"Agent{i}\", llm_config=False) for i in range(5)]\n",
    "allowed_speaker_transitions_dict = {\n",
    "    agents[0]: [agents[1], agents[2], agents[3], agents[4]],\n",
    "    agents[1]: [agents[0]],\n",
    "    agents[2]: [agents[0]],\n",
    "    agents[3]: [agents[0]],\n",
    "    agents[4]: [agents[0]],\n",
    "}\n",
    "\n",
    "visualize_speaker_transitions_dict(allowed_speaker_transitions_dict, agents)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Sequential Team Operations\n",
    "# Create an empty directed graph\n",
    "\n",
    "speaker_transitions_dict = {}\n",
    "teams = [\"A\", \"B\", \"C\"]\n",
    "team_size = 5\n",
    "\n",
    "\n",
    "def get_agent_of_name(agents, name) -> ConversableAgent:\n",
    "    for agent in agents:\n",
    "        if agent.name == name:\n",
    "            return agent\n",
    "\n",
    "\n",
    "# Create a list of 15 agents 3 teams x 5 agents\n",
    "agents = [ConversableAgent(name=f\"{team}{i}\", llm_config=False) for team in teams for i in range(team_size)]\n",
    "\n",
    "# Loop through each team and add members and their connections\n",
    "for team in teams:\n",
    "    for i in range(team_size):\n",
    "        member = f\"{team}{i}\"\n",
    "        # Connect each member to other members of the same team\n",
    "        speaker_transitions_dict[get_agent_of_name(agents, member)] = [\n",
    "            get_agent_of_name(agents, name=f\"{team}{j}\") for j in range(team_size) if j != i\n",
    "        ]\n",
    "\n",
    "# Team leaders connection\n",
    "print(get_agent_of_name(agents, name=\"B0\"))\n",
    "speaker_transitions_dict[get_agent_of_name(agents, \"A0\")].append(get_agent_of_name(agents, name=\"B0\"))\n",
    "speaker_transitions_dict[get_agent_of_name(agents, \"B0\")].append(get_agent_of_name(agents, name=\"C0\"))\n",
    "\n",
    "visualize_speaker_transitions_dict(speaker_transitions_dict, agents)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "agents = [ConversableAgent(name=f\"Agent{i}\", llm_config=False) for i in range(2)]\n",
    "allowed_speaker_transitions_dict = {\n",
    "    agents[0]: [agents[0], agents[1]],\n",
    "    agents[1]: [agents[0], agents[1]],\n",
    "}\n",
    "\n",
    "visualize_speaker_transitions_dict(allowed_speaker_transitions_dict, agents)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Demonstration"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`GroupChat` now takes in two optional arguments.\n",
    "- allowed_or_disallowed_speaker_transitions: The keys are source agents, and the values are agents that the key agent can/can't transit to, depending on speaker_transitions_type. Default is None, which means all agents can transit to all other agents.\n",
    "- speaker_transitions_type: whether the speaker_transitions_type is a dictionary containing lists of allowed agents or disallowed agents. \"allowed\" means the `allowed_or_disallowed_speaker_transitions` is a dictionary containing lists of allowed agents."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Team Operations\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create an empty directed graph\n",
    "agents = []\n",
    "speaker_transitions_dict = {}\n",
    "secret_values = {}\n",
    "\n",
    "# Outer loop for prefixes 'A', 'B', 'C'\n",
    "for prefix in [\"A\", \"B\", \"C\"]:\n",
    "    # Add 3 nodes with each prefix to the graph using a for loop\n",
    "    for i in range(3):\n",
    "        node_id = f\"{prefix}{i}\"\n",
    "        secret_value = random.randint(1, 5)  # Generate a random secret value\n",
    "        secret_values[node_id] = secret_value\n",
    "\n",
    "        # Create an AssistantAgent for each node (assuming AssistantAgent is a defined class)\n",
    "        agents.append(\n",
    "            AssistantAgent(\n",
    "                name=node_id,\n",
    "                llm_config=llm_config,\n",
    "                system_message=f\"\"\"Your name is {node_id}.\n",
    "                                        Do not respond as the speaker named in the NEXT tag if your name is not in the NEXT tag. Instead, suggest a relevant team leader to handle the mis-tag, with the NEXT: tag.\n",
    "\n",
    "                                        You have {secret_value} chocolates.\n",
    "\n",
    "                                        The list of players are [A0, A1, A2, B0, B1, B2, C0, C1, C2].\n",
    "\n",
    "                                            Your first character of your name is your team, and your second character denotes that you are a team leader if it is 0.\n",
    "                                            CONSTRAINTS: Team members can only talk within the team, whilst team leader can talk to team leaders of other teams but not team members of other teams.\n",
    "\n",
    "                                            You can use NEXT: to suggest the next speaker. You have to respect the CONSTRAINTS, and can only suggest one player from the list of players, i.e., do not suggest A3 because A3 is not from the list of players.\n",
    "                                            Team leaders must make sure that they know the sum of the individual chocolate count of all three players in their own team, i.e., A0 is responsible for team A only.\n",
    "\n",
    "                                        Keep track of the player's tally using a JSON format so that others can check the total tally. Use\n",
    "                                        A0:?, A1:?, A2:?,\n",
    "                                        B0:?, B1:?, B2:?,\n",
    "                                        C0:?, C1:?, C2:?\n",
    "\n",
    "                                        If you are the team leader, you should aggregate your team's total chocolate count to cooperate.\n",
    "                                        Once the team leader know their team's tally, they can suggest another team leader for them to find their team tally, because we need all three team tallys to succeed.\n",
    "                                        Use NEXT: to suggest the next speaker, e.g., NEXT: A0.\n",
    "\n",
    "                                        Once we have the total tally from all nine players, sum up all three teams' tally, then terminate the discussion using TERMINATE.\n",
    "\n",
    "                                        \"\"\",\n",
    "            )\n",
    "        )\n",
    "        speaker_transitions_dict[agents[-1]] = []\n",
    "\n",
    "    # Add edges between nodes with the same prefix using a nested for loop\n",
    "    for source_node in range(3):\n",
    "        source_id = f\"{prefix}{source_node}\"\n",
    "        for target_node in range(3):\n",
    "            target_id = f\"{prefix}{target_node}\"\n",
    "            if source_node != target_node:  # To avoid self-loops\n",
    "                speaker_transitions_dict[get_agent_of_name(agents, source_id)].append(\n",
    "                    get_agent_of_name(agents, name=target_id)\n",
    "                )\n",
    "\n",
    "\n",
    "# Adding edges between teams\n",
    "speaker_transitions_dict[get_agent_of_name(agents, \"A0\")].append(get_agent_of_name(agents, name=\"B0\"))\n",
    "speaker_transitions_dict[get_agent_of_name(agents, \"A0\")].append(get_agent_of_name(agents, name=\"C0\"))\n",
    "speaker_transitions_dict[get_agent_of_name(agents, \"B0\")].append(get_agent_of_name(agents, name=\"A0\"))\n",
    "speaker_transitions_dict[get_agent_of_name(agents, \"B0\")].append(get_agent_of_name(agents, name=\"C0\"))\n",
    "speaker_transitions_dict[get_agent_of_name(agents, \"C0\")].append(get_agent_of_name(agents, name=\"A0\"))\n",
    "speaker_transitions_dict[get_agent_of_name(agents, \"C0\")].append(get_agent_of_name(agents, name=\"B0\"))\n",
    "\n",
    "\n",
    "# Visualization only\n",
    "graph = nx.DiGraph()\n",
    "\n",
    "# Add nodes\n",
    "graph.add_nodes_from([agent.name for agent in agents])\n",
    "\n",
    "# Add edges\n",
    "for key, value in speaker_transitions_dict.items():\n",
    "    for agent in value:\n",
    "        graph.add_edge(key.name, agent.name)\n",
    "\n",
    "# Visualize\n",
    "# Draw the graph with secret values annotated\n",
    "plt.figure(figsize=(12, 10))\n",
    "pos = nx.spring_layout(graph)  # positions for all nodes\n",
    "\n",
    "# Draw nodes with their colors\n",
    "nx.draw(graph, pos, with_labels=True, font_weight=\"bold\")\n",
    "\n",
    "# Annotate secret values\n",
    "for node, (x, y) in pos.items():\n",
    "    secret_value = secret_values[node]\n",
    "    plt.text(x, y + 0.1, s=f\"Secret: {secret_value}\", horizontalalignment=\"center\")\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Termination message detection\n",
    "\n",
    "\n",
    "def is_termination_msg(content) -> bool:\n",
    "    have_content = content.get(\"content\", None) is not None\n",
    "    return have_content and \"TERMINATE\" in content[\"content\"]\n",
    "\n",
    "\n",
    "# Terminates the conversation when TERMINATE is detected.\n",
    "user_proxy = autogen.UserProxyAgent(\n",
    "    name=\"User_proxy\",\n",
    "    system_message=\"Terminator admin.\",\n",
    "    code_execution_config=False,\n",
    "    is_termination_msg=is_termination_msg,\n",
    "    human_input_mode=\"NEVER\",\n",
    ")\n",
    "\n",
    "agents.append(user_proxy)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "group_chat = GroupChat(\n",
    "    agents=agents,\n",
    "    messages=[],\n",
    "    max_round=20,\n",
    "    allowed_or_disallowed_speaker_transitions=speaker_transitions_dict,\n",
    "    speaker_transitions_type=\"allowed\",\n",
    ")\n",
    "\n",
    "\n",
    "# Create the manager\n",
    "manager = autogen.GroupChatManager(\n",
    "    groupchat=group_chat,\n",
    "    code_execution_config=False,\n",
    "    is_termination_msg=is_termination_msg,\n",
    "    llm_config=llm_config,\n",
    ")\n",
    "\n",
    "\n",
    "# Initiates the chat with Alice\n",
    "agents[0].initiate_chat(\n",
    "    manager,\n",
    "    message=\"\"\"\n",
    "                        There are 9 players in this game, split equally into Teams A, B, C. Therefore each team has 3 players, including the team leader.\n",
    "                        The task is to find out the sum of chocolate counts from all nine players. I will now start with my team.\n",
    "                        NEXT: A1\"\"\",\n",
    ")"
   ]
  }
 ],
 "metadata": {
  "front_matter": {
   "description": "Explore the demonstration of the Finite State Machine implementation, which allows the user to input speaker transition constraints.",
   "tags": [
    "group chat",
    "fsm",
    "orchestration"
   ]
  },
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
